<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来访体温" prop="ltemp">
        <el-input
          v-model="queryParams.ltemp"
          placeholder="请输入来访体温"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出发地点" prop="lwhere">
        <el-input
          v-model="queryParams.lwhere"
          placeholder="请输入出发地点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来访原因" prop="lreason">
        <el-input
          v-model="queryParams.lreason"
          placeholder="请输入来访原因"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来访时间" prop="ltime">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.ltime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择来访时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="comeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="来访时间" align="center" prop="id"/>
      <el-table-column label="姓名" align="center" prop="name"/>
      <el-table-column label="来访体温" align="center" prop="ltemp"/>
      <el-table-column label="出发地点" align="center" prop="lwhere"/>
      <el-table-column label="来访原因" align="center" prop="lreason"/>
      <el-table-column label="来访时间" align="center" prop="ltime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ltime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-marketing"
            @click="handleDelete1(scope.row)"
          >查看体温
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改来访者信息对话框 -->
    <el-dialog :title="title1" :visible.sync="open1" width="60%" @opened="initChart()" append-to-body>
      <template>
        <div class="proCharts" ref="hygrometer"></div>
      </template>

    </el-dialog>
    <!-- 添加或修改来访者信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="来访体温" prop="ltemp">
          <el-input v-model="form.ltemp" placeholder="请输入来访体温"/>
        </el-form-item>
        <el-form-item label="出发地点" prop="lwhere">
          <el-input v-model="form.lwhere" placeholder="请输入出发地点"/>
        </el-form-item>
        <el-form-item label="来访原因" prop="lreason">
          <el-input v-model="form.lreason" placeholder="请输入来访原因"/>
        </el-form-item>
        <el-form-item label="来访时间" prop="ltime">
          <el-date-picker clearable :picker-options="expireTimeOption" size="small" style="width: 200px"
                          v-model="form.ltime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择来访时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCome, getCome, delCome, addCome, updateCome, exportCome } from '@/api/emsboot/come'

export default {
  name: 'Come',
  components: {},
  data() {
    return {
      expireTimeOption: {
        disabledDate(date) {
          //disabledDate 文档上：设置禁用状态，参数为当前日期，要求返回 Boolean
          return date.getTime() < Date.now() - 24 * 60 * 60 * 1000
        }
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      title1: 'title1',
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      open1: false,
      // 总条数
      total: 0,
      // 来访者信息表格数据
      comeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        ltemp: null,
        lwhere: null,
        lreason: null,
        ltime: null
      },
      // 表单参数
      form: {},
      option: {
        color: ['rgb(8,252,7)', 'rgb(255,168,0)'],
        title: {
          text: ''
        },
        legend: {//图例的类型
          icon: 'roundRect',//图例icon图标
          data: [
            {
              name: '体温',
              textStyle: {
                color: '#fff'
              }

            }
            // ,{
            //   name:"本年",
            //   textStyle: {
            //     color: '#fff'
            //   }
            // },
          ]

        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          top: '17%',
          containLabel: true //grid区域是否包含坐标轴的刻度标签
        },
        xAxis: {
          type: 'category', //坐标轴类型。
          boundaryGap: false, //坐标轴两边留白策略
          data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
          axisLabel: {//坐标轴刻度标签的相关设置
            interval: 0,
            textStyle: {
              color: '#fff',
              fontSize: 10
            }
          },
          axisLine: {//坐标轴轴线相关设置
            show: true,
            lineStyle: {
              color: 'rgb(2,121,253)'
            }
          },
          axisTick: { //坐标轴刻度相关设置。
            show: false
          }
        },
        yAxis: {
          type: 'value',
          axisLabel: { //x轴的坐标文字
            show: true,
            textStyle: {
              color: '#fff' //文字的颜色
            },
            formatter: '{value} °C'
          },
          // max:100,//最大值100
          axisLine: {//坐标轴轴线相关设置
            show: true,
            lineStyle: {
              color: 'rgb(2,121,253)'
            }
          },
          axisTick: { //坐标轴刻度相关设置。
            show: false
          },
          splitLine: {  //坐标在grid区域的分割线
            lineStyle: { //设置分割线的样式(图表横线颜色)
              color: ['#153a8a']
            }
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },

        series: [
          {
            type: 'line',
            data: [10, 20, 30, 50, 50, 10, 50, 60, 10, 50, 10, 30],
            lineStyle: {
              color: 'rgb(8,252,7)'  //线的颜色
            }
          }
          // {
          //   name: '本年',
          //   type: 'line',
          //   data: [20,20,30,50,50,10,50,20,30,50,50,30],
          //   lineStyle:{
          //     color:'rgb(255,168,0)' //线的颜色
          //   }
          // }
        ]

      },

      // 表单校验
      rules: {
        ltime:
          {
            required: true,
            message: '请选择时间',
            trigger: 'blur'
          }

      }
    }
  },
  created() {
    this.getList()

  },

  methods: {

    randArray(len, min, max) {
      return Array.from({ length: len }, v => Math.floor(Math.random() * (max - min)) + min)
    }
    ,
    initChart() {

      var count = Math.floor(Math.random() * 14 + 3)    // 可均衡获取 0 到 9 的随机整数。

      var arr = []

      for (let i = 0; i < count; i++) {
        arr.unshift(this.$moment(new Date() - 86400000 * i).format('YYYY-MM-DD'))

      }

      this.option.series[0].data = this.randArray(count, 36.5, 37.7)

      this.option.xAxis.data = arr
      let myChart = this.$echarts.init(this.$refs.hygrometer, 'macarons')
      myChart.setOption(this.option)
      //图表自适应
      window.addEventListener('resize', function() {
        myChart.resize()// myChart 是实例对象
      })
    }
    ,
    handleDelete1() {
      this.open1 = true
      this.title1 = '体温变化图'

    }
    ,
    /** 查询来访者信息列表 */
    getList() {
      this.loading = true
      listCome(this.queryParams).then(response => {
        this.comeList = response.rows
        this.total = response.total
        this.loading = false
      })
    }
    ,
// 取消按钮
    cancel() {
      this.open = false
      this.reset()
    }
    ,
// 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        ltemp: null,
        lwhere: null,
        lreason: null,
        ltime: null
      }
      this.resetForm('form')
    }
    ,
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    }
    ,
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    }
    ,
// 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    }
    ,
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加来访者信息'
    }
    ,
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCome(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改来访者信息'
      })
    }
    ,
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCome(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addCome(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    }
    ,
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除来访者信息编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delCome(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    }
  }
}
</script>
<style>

.proCharts {
  width: 100%;
  height: 400px;
  background: rgb(14, 51, 129);
}
</style>
